package com.example.shushiyunsst.fragment.main.transaction

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.drawerlayout.widget.DrawerLayout
import com.alibaba.fastjson.JSON
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.networking.RestClient
import com.example.library_common.utils.QMUIDisplayHelper
import com.example.library_common.utils.even.MessageWrap
import com.example.library_common.view.bottombar.BottomBorInfo
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.transaction.bean.TransIndexBean
import com.example.shushiyunsst.fragment.main.transaction.bean.TransactionUIInvokeListener
import com.example.shushiyunsst.fragment.main.transaction.child.*
import com.example.wallet.db.UserInfoManager
import com.example.wallet.utils.selectorIcon
import org.greenrobot.eventbus.EventBus

class TransactionFragment : BaseFragmentEx(), BottomBorInfo, TransactionUIInvokeListener {

    companion object {
        fun newInstance(): TransactionFragment {
            val args = Bundle()

            val fragment = TransactionFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override var icon = R.drawable.ic_transaction

    private val contentFragment = TransactionContentFragment.newInstance()

    private var isInitView=false

    override var title = R.string.transaction

    private lateinit var mDrawerLayout: DrawerLayout

    private var indexNode = TransIndexNode()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
    }

    override fun getLayoutId() = R.layout.transaction_fragment_layout

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        mDrawerLayout = view.findViewById(R.id.mDrawerLayout)

        val menus = view.findViewById<View>(R.id.menus)
        val params = menus.layoutParams
        params.width = QMUIDisplayHelper.getScreenWidth(_mActivity) * 2 / 3
        menus.layoutParams = params


    }

    override fun onLazyInitView(savedInstanceState: Bundle?) {
        super.onLazyInitView(savedInstanceState)
        if (savedInstanceState == null) {
            isInitView=true
            val menusFragment = TransactionMenuFragment.newInstance()
                .setTransactionUIInvokeListener(this)

            loadRootFragment(R.id.menus, menusFragment)

            contentFragment.setTransactionUIInvokeListener(this)

            loadRootFragment(R.id.content, contentFragment)
        }
    }

    override fun handMessage(msg: MessageWrap?) {

        if (msg?.message == transfer_index) {
            requestIndex()
        }
    }

    override fun onSupportVisible() {
        super.onSupportVisible()
        if (isInitView){
            refreshData()
        }
    }

    override fun refreshData() {
        if (mDrawerLayout.isOpen) {
            mDrawerLayout.close()
        }
        contentFragment.refresh()
    }

    override fun toggleDrawerLayout() {
        if (mDrawerLayout.isOpen) {
            mDrawerLayout.close()
        } else {
            mDrawerLayout.open()
        }
    }

    override fun closeDrawerLayout() {
        if (mDrawerLayout.isOpen) {
            mDrawerLayout.close()
        }
    }

    override fun requestIndex() {
        val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo
        val tokenType = indexNode.iconType?.tokenName ?: selectorIcon()
        val txType = indexNode.transType

        val map = mapOf(
            "tokenType" to tokenType,
            "txType" to txType,
            "uid" to userInfo?.uid,
//            "uid" to "126",
        )
        RestClient.builder()
            .raw(JSON.toJSONString(map))
            .url(R.string.queryTransTxIndex)
            .success {

                parseQueryTransTxIndex(it)
            }
            .error { _, msg ->
                Toast.makeText(_mActivity, msg,Toast.LENGTH_LONG).show()
                EventBus.getDefault().post(MessageWrap.getInstance(transfer_index_data_failed))
            }.failure {
                EventBus.getDefault().post(MessageWrap.getInstance(transfer_index_data_failed))
            }
            .build().post()
    }

    private fun parseQueryTransTxIndex(it: String?) {
        val parseObject = JSON.parseObject(it, TransIndexBean::class.java)
        indexNode.bean = parseObject
        val transferIndexData = MessageWrap.getInstance(transfer_index_data)
        EventBus.getDefault().post(transferIndexData)
    }

    override fun getTransIndexNode(): TransIndexNode {
        return indexNode
    }
}